package br.com.sge.controle;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import br.com.sge.entidades.Disciplina;
import br.com.sge.servicos.DisciplinaServico;
import br.com.sge.servicosimp.DisciplinaServicoImp;

/**
 *
 * @author Gustavo Rodrigues Torre
 */
@ManagedBean
@SessionScoped
public class DisciplinaManagedBeans {

    private Disciplina disciplina;
    private DataModel listaDisciplinas;

    public DataModel getListarDisciplinas() {
        List<Disciplina> lista = new DisciplinaServicoImp().list();
        listaDisciplinas = new ListDataModel(lista);
        return listaDisciplinas;
    }

    public Disciplina getDisciplina() {
        return disciplina;
    }

    public void setDisciplina(Disciplina disciplina) {
        this.disciplina = disciplina;
    }

    public void prepararAdicionarDisciplina(ActionEvent actionEvent){
        disciplina = new Disciplina();
    }

    public void prepararAlterarDisciplina(ActionEvent actionEvent){
        disciplina = (Disciplina)(listaDisciplinas.getRowData());
    }

    public String excluirDisciplina(){

        Disciplina disciplinaTemp = (Disciplina)(listaDisciplinas.getRowData());
        DisciplinaServico dao = new DisciplinaServicoImp();
        dao.remove(disciplinaTemp);
        return "disciplinas";

    }

    public void adicionarDisciplina(ActionEvent actionEvent){

        DisciplinaServico dao = new DisciplinaServicoImp();
        dao.save(disciplina);
        
    }

    public void alterarDisciplina(ActionEvent actionEvent){

        DisciplinaServico dao = new DisciplinaServicoImp();
        dao.update(disciplina);

    }

}
